<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2017/10/23 0023
  Time: 上午 11:02
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<link rel="stylesheet" href="/static/plugins/layui/css/layui.css">
<link rel="stylesheet" href="/static/css/commom.css">
<html>
<head>
    <title>添加权限</title>
</head>
<body>
<div>
    <span class="layui-breadcrumb">
        <a href="/" target="_top">首页</a>
        <a href="/resource">权限管理</a>
        <a><cite>添加</cite></a>
    </span>
</div>
<div class="form-style">
    <form class="layui-form" action="/resource/save" method="post">
        <div class="layui-form-item">
            <label class="layui-form-label">权限名称</label>
            <div class="layui-input-block">
                <input type="text" name="permisName"   lay-verify="required" placeholder="请输入权限名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" id="typeDiv">
            <label class="layui-form-label">类型</label>
            <div class="layui-input-block">
                <select name="permisType" id="type"  lay-filter="type" lay-verify="required">
                    <option value="-1">--请选择权限类型--</option>
                    <option value="1">模块</option>
                    <option value="2">功能</option>
                    <option value="3">按钮</option>
                    <option value="4">附属操作</option>
                    <option value="9">默认权限</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item" id="uriDiv" style="display: none">
            <label class="layui-form-label">URI</label>
            <div class="layui-input-block">
                <input type="text" name="relateUrl"  placeholder="请输入uri" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" id="parentIdDiv" style="display: none">
            <label class="layui-form-label">父级权限</label>
            <div class="layui-input-block">
                <select name="parentId" id="parentItem">
                </select>
            </div>
        </div>
        <div class="layui-form-item" id="btnIdDiv" style="display: none">
            <label class="layui-form-label">按钮id</label>
            <div class="layui-input-block">
                <input type="text" name="btnId"  lay-verify="required" placeholder="请输入按钮id" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" >
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit lay-filter="formDemo">完成</button>
                <a href="/resource"><button type="button" class="layui-btn layui-btn-primary">取消</button></a>
            </div>
        </div>
    </form>
</div>

<script src="/static/plugins/jquery.js"></script>
<script src="/static/plugins/layui/layui.js"></script>
<script>
    layui.use(['form','element'], function(){
        var form = layui.form;
        form.on('select(type)', function(data){
            var val = data.value;
            if (val>1){
                $('#parentIdDiv').show();
                $('#parentIdDiv').find('select').attr('lay-verify','required');
                if (val ==9) {
                    $('#parentIdDiv').hide();
                    $('#parentIdDiv').find('select').removeAttr('lay-verify');
                }
                if(val ==3){
                    $('#btnIdDiv').show();
                    $('#btnIdDiv').find('input').attr('lay-verify','required');
                }else {
                    $('#btnIdDiv').hide();
                    $('#btnIdDiv').find('input').removeAttr('lay-verify');
                }
                $('#uriDiv').show();
                $('#uriDiv').find('input').attr('lay-verify','required');

            }else {
                $('#uriDiv').hide();
                $('#uriDiv').find('input').removeAttr('lay-verify');
                $('#btnIdDiv').hide();
                $('#btnIdDiv').find('input').removeAttr('lay-verify');
                $('#parentIdDiv').hide();
                $('#parentIdDiv').find('select').removeAttr('lay-verify');
            }

            if (val>1&&val<5){
                debugger;
                $("#parentItem").empty();
                $.ajax({
                    url:"/resource/type/list",
                    type:"get",
                    data:{type:val==2?1:2},
                    dataType:"json",
                    success:function (data) {
                        $.each(data.data,function (i,item) {
                            $("#parentItem").append("<option value='"+item.permisId+"'>"+item.permisName+"</option>");
                        })
                        form.render('select'); //刷新select选择框渲染
                    },
                    error:function () {
                        console.log(data.message);
                    }
                })
            }
        });
    });
</script>
</body>
</html>
